﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utils;

namespace DevDemo
{
    public partial class MainForm : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        string sError = string.Empty;
        public MainForm()
        {
            InitializeComponent();
        }
        private void InitGrid()
        {

            SqlLiteHelper.ConnSqlLiteDbPath = @"data\testdb.db";



            sError = string.Empty;

            string sSql = "select * from task_TB";

            DataTable dt = SqlLiteHelper.GetDataTable(out sError, sSql);

            if (!string.IsNullOrEmpty(sError))

                Common.DisplayMsg(this.Text, sError);



            dataGridView1.DataSource = dt;

        }
        

        private void MainForm_Load(object sender, EventArgs e)
        {
           

            InitGrid();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sError = string.Empty;

            int iMaxID = SqlLiteHelper.GetMaxID(out sError, "id", "task_tb") + 1;

            string sSql = "insert into task_tb select " + "'taskid" +iMaxID + "'"+", 'taskname" + iMaxID + "',"+iMaxID ;

            sError = string.Empty;

            bool bResult = SqlLiteHelper.UpdateData(out sError, sSql, true);

            if (bResult)

                Common.DisplayMsg(this.Text, "插入成功");



            InitGrid();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sError = string.Empty;

            int iMaxID = SqlLiteHelper.GetMaxID(out sError, "id", "task_TB");

            string sSql = "update task_TB set taskname='name_jonse',taskid='remark_jonse' where id=" +



iMaxID;

            sError = string.Empty;

            bool bResult = SqlLiteHelper.UpdateData(out sError, sSql, true);

            if (bResult)

                Common.DisplayMsg(this.Text, "修改成功");



            InitGrid();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            sError = string.Empty;

            int iMaxID = SqlLiteHelper.GetMaxID(out sError, "id", "task_TB");

            string sSql = "delete from task_TB where id=" + iMaxID;

            sError = string.Empty;

            bool bResult = SqlLiteHelper.UpdateData(out sError, sSql, true);

            if (bResult)

                Common.DisplayMsg(this.Text, "删除成功");



            InitGrid();
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        } 

 
    }
    public abstract class Common
    {

        public static void DisplayMsg(string sCaption, string sMsg)
        {

            sMsg = sMsg.TrimEnd('!').TrimEnd('！') + " !";

            MessageBox.Show(sMsg, sCaption);

        }



        public static bool IsNullOrEmptyObject(object oSource)
        {

            if (oSource != null)
            {

                return string.IsNullOrEmpty(oSource.ToString());

            }



            return true;

        }



    }
}
